<?php
session_start();

//echo "<pre>";
//print_r($_SESSION);
//print_r($data);exit;
require 'include/config.inc.php';
include FB_ABSPATH."/db/opendb.php";
//print_r($_POST);exit;
if(isset($_POST['btnSubmit_x'])) {

	$username='';
	$password='';
	if(isset($_POST['username']))
	$username = $_POST['username'];
	if(isset($_POST['password']))
	$password = md5($_POST['password']);

	if($username && $password)
	{
		include "../db/opendb.php";
		$query = "select Verified, Role, Password, ID from Users where Username='".$username."'";
		//echo $query;
		$result = mysql_query($query) or die ("SQL Error".mysql_error());
		$row=mysql_fetch_assoc($result);
		if(!$row['Password'] || $row['Password'] !=$password)
		{
			$err_msg = "The login information you have supplied is incorrect!";
			//include "information.php";
		}
		else if($row['Verified'] != '1')
		{
			$err_msg = "This user account is not verified!";
		}
		else
		{
			$_SESSION['username']=$username;
			$_SESSION['role']=$row["Role"];
			$user_id = $_SESSION['id']=$row["ID"];
			//set the type session variable : basic/single/starter/pro/power
			$query = "select Type from Users where Username='".$username."'";
			//echo $query;
			$result = mysql_query($query) or die ("SQL Error".mysql_error());
			$row=mysql_fetch_assoc($result);
			$_SESSION['type']=$row['Type'];

			//Now check if user has fb user id on our main application
			$fb_signed_id = $_REQUEST['fb_user_id'];

			$sql = "SELECT * FROM facebook_user_map WHERE fb_signed_id='$fb_signed_id'";
			$rs = mysql_query($sql);
			if(mysql_num_rows($rs) < 1) {
				//add id
				$sql = "INSERT IGNORE INTO facebook_user_map (user_id, fb_signed_id) VALUES ('$user_id','$fb_signed_id')";
				$rs = mysql_query($sql);
			}
			$err_msg = "YES";
		}
		echo $err_msg;exit;
	}
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title><?php echo SITE_NAME?> - Local Real Estate Classifieds </title>
<meta name="description" content="Local Real Estate, Apartments, Realtors, Brokers, Real Estate Agents">
<meta name="keywords" content="Real Estate,Realtor,Agent,Broker,Find a Agent">
<link href="<?php echo SITE_URL;?>/style/StyleSheetV2.css" type="text/css" rel="stylesheet">

</head>
<body style="background-color: rgb(255, 255, 255);">
<form action="<?php echo SITE_URL?>/facebook/assign_user.php" method="post" name="login">
<input type="hidden" name="fb_user_id" value="<?php echo $_REQUEST['fb_user_id']?>">
<div style="background-color: rgb(255, 255, 255);" align="center">
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>

    <p>&nbsp;</p>
    <div style="width: 850px; background-color: rgb(255, 255, 255); padding-bottom: 40px;" align="center">
            <table 10="" cellpadding="" width="100%">
                <tbody><tr>
                    <td>
	                      <div><img src="<?php echo SITE_URL;?>/facebook/style/realivent_logo.gif"></div>
                    </td>
                    <td style="font-family: lucida grande,tahoma,verdana,arial,sans-serif; font-size: 35px; font-weight: normal; color: rgb(102, 102, 102); padding-bottom: 40px; padding-left: 40px;">
                        Sign in
                    </td>

                </tr>
                <?php
                if($err_msg) {
                	echo "<tr><td></td><td style='color:red;padding-left: 40px;'>$err_msg</td></tr>";
                }
                ?>
                <tr>
                    <td valign="top">
                        <div>
                            <table>
                                <tbody><tr>
                                    <td>
                                    </td>
                                    <td style="font-family: lucida grande,tahoma,verdana,arial,sans-serif; font-size: 30px; font-weight: normal; color: rgb(102, 102, 102); padding-bottom: 40px;">

                                        <div style="">
                                            Local Real Estate<br>
                                            Classifieds</div>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 17px;" valign="top">
                                        <img src="<?php echo SITE_URL;?>/facebook/style/realivent_SpSquare.gif" id="Squar1">

                                    </td>
                                    <td style="font-family: lucida grande,tahoma,verdana,arial,sans-serif; font-size: 12px; font-weight: normal; color: rgb(102, 102, 102); padding-bottom: 10px;">
                                        Advertise your company and generate leads from around the world!
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 17px;" valign="top">
                                        <img src="<?php echo SITE_URL;?>/facebook/style/realivent_SpSquare.gif" id="Squar12">
                                    </td>

                                    <td style="font-family: lucida grande,tahoma,verdana,arial,sans-serif; font-size: 12px; font-weight: normal; color: rgb(102, 102, 102); padding-bottom: 10px;">
                                        <b>Enjoy our New features</b> to link all your REALIVENT listings to your FACEBOOK Business/Fan pages.<br>
                                        <a href="<?php echo SITE_URL?>/facebookApp/Overview.aspx" id="A4" style="color: rgb(51, 102, 153);">Learn more</a>
                                    </td>
                                </tr>
                            </tbody></table>
                        </div>

                        <div>
                            <table cellpadding="10">
                                <tbody><tr>
                                    <td style="font-family: lucida grande,tahoma,verdana,arial,sans-serif; font-size: 16px; font-weight: normal; color: rgb(59, 89, 152);">
                                        Don't have a <?php echo SITE_NAME?> Account?
                                    </td>
                                    <td>
                                        <a title="New Member Sign Up" href="<?php echo SIGNUP_URL?>">
                                            <img src="<?php echo SITE_URL;?>/facebook/style/realivent_SignUpBtn.gif" border="0"></a>

                                    </td>
                                </tr>
                            </tbody></table>
                        </div>
                    </td>
                    <td style="width: 400px; padding-left: 40px;" valign="top">
                        <div style="background-color: rgb(242, 246, 253); border: 1px solid rgb(186, 216, 251); padding: 3px;">
                            <div id="pnlLogin" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'btnSubmit')">
	
                                <table>

                                    <tbody><tr>
                                        <td valign="top">
                                            <img src="<?php echo SITE_URL;?>/facebook/style/realivent_LoginIcon.gif">
                                        </td>
                                        <td>
                                            <div>
                                                <span id="lblMsg" style="color: rgb(255, 0, 51);"></span><br>
                                            </div>
                                            <div style="font-family: lucida grande,tahoma,verdana,arial,sans-serif; font-size: 12px; font-weight: normal; color: rgb(102, 102, 102);">

                                                User Name</div>
                                            <div>
                                                <input name="username" maxlength="50" id="txtEmail" style="width: 200px;" type="text">
                                                <span id="rfvEmail" style="color: Red; display: none;">* Required</span>
                                                <span id="rfvEmailAddress" style="color: Red; display: none;">* Invalid email address</span></div>
                                            <div style="font-family: lucida grande,tahoma,verdana,arial,sans-serif; font-size: 12px; font-weight: normal; color: rgb(102, 102, 102);">
                                                Password</div>

                                            <div>
                                                <input name="password" maxlength="15" id="txtPassword" style="width: 200px;" type="password">
                                                <span id="rfvPassword" style="color: Red; visibility: hidden;">* Required</span></div>
                                            <div>
                                                <a href="<?php echo SITE_URL?>/members/GetPassword.aspx" style="font-family: lucida grande,tahoma,verdana,arial,sans-serif; font-size: 11px; font-weight: normal; color: rgb(0, 102, 204);">Forgot your
                                                    password?</a></div>
                                                    <br>
                                                    <br>
                                            <div style="text-align: right;">

                                                <input name="btnSubmit" id="btnSubmit" src="<?php echo SITE_URL;?>/facebook/style/realivent_SignInBtn.gif" style="border-style: none; border-width: 0px;" type="image"></div>
                                        </td>
                                    </tr>
                                </tbody></table>
                            
</div>
                        </div>
                    </td>
                </tr>
            </tbody></table>

   </div>
        </div>
    <div style="padding-bottom: 3px; background-color: rgb(255, 255, 255);" align="center">
        <img src="<?php echo SITE_URL;?>/facebook/style/realivent_HP_Footer.jpg" id="Img3"></div>
</form>
</body>
</html>